import{api,env} from '../config/config.js'
var NODE_ENV = env
var configApi = api
const request = {
	interfaceUrl: function() {
		if(NODE_ENV === 'deveploment'){
			return configApi.base
		}
	},
	getToken() {
		let userInfo = uni.getStorage('Token')
		return userInfo.token
	},
	toast: function(text, success) {
		uni.showToast({
			title: text || "出错了",
			icon: success ? 'success' : 'none',
			duration: 2000
		})
	},
	// 响应拦截器
	lsxmRequest:function(statuCode){
		const code = statuCode
		switch(code){
			case 401 :
			request.toast("请求被拦截")
			uni.navigateTo({
				url: '/pages/login/login'
			})
			break
			case 200 :
			request.toast("请求成功")
		}
	},
	// post请求
	request: function(
		url,
		data,
	) {
		return new Promise(function(reslove, reject) {
			// console.log(request.interfaceUrl())
			uni.request({
				url: request.interfaceUrl() + url,
				// url:url,
				data: data,
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					// 'token': request.getToken()
					'token': 'asdf123456789'
				},
				method: 'POST',
				dataType: 'json',
				success: (res) => {
					request.lsxmRequest(res.statusCode)
					reslove(res)
				},
				fail: (res) => {
					request.toast("网络不给力，请稍后再试~")
					reject(res)
				}
			})
		})
	},
	// get请求
	get:function(url){
		return new Promise(function(reslove,reject){
			// console.log(request.interfaceUrl())
			uni.request({
				url:request.interfaceUrl()+url,
				method:'GET',
				dataType:'json',
				success: (res) => {
					request.lsxmRequest(res.statusCode)
					reslove(res)
				},
				fail: (res) => {
					request.toast("网络不给力，请稍后再试~")
					reject(res)
				}
			})
		})
	},
	// 
}
export default request
